namespace Launcher.View.Model

import System

class Paginator:
"""Description of Paginator"""

	_numberOfItemsInFirstLine as int
	_maxNumberOfLines as int
	
	[property(Applications)]
	_apps = []

	def constructor():
		pass
		
	def Paginate(numberOfItemsInFirstLine as int, maxNumberOfLines as int):
		_numberOfItemsInFirstLine = numberOfItemsInFirstLine
		_maxNumberOfLines = maxNumberOfLines
				
		numberOfModelItems = _apps.Count
		numberOfItems = CalculateNumberOfItems(numberOfItemsInFirstLine, maxNumberOfLines)
		numberOfPages = numberOfModelItems / numberOfItems
		if (numberOfModelItems % numberOfItems) != 0:
			numberOfPages++
			
		if numberOfPages == 0:
			numberOfPages++

		return [CreatePage(i, numberOfItems) for i in range(numberOfPages)]
			
	private def CalculateNumberOfItems(numberOfItemsInFirstLine as int, maxNumberOfLines as int):
		return 1 + numberOfItemsInFirstLine * CalculateNumberOfBlocks(maxNumberOfLines)
		
	private def CalculateNumberOfBlocks(numberOfLines as int) as int:
		if numberOfLines == 1: return 1
		return CalculateNumberOfBlocks(numberOfLines - 1) + numberOfLines
		
	private def CreatePage(pageNumber as int, numberOfItems as int):
		startPos = pageNumber * numberOfItems
		endPos = startPos + numberOfItems
		if endPos > len(_apps): endPos = len(_apps)

		page = LauncherViewPage(_apps[startPos: endPos])
		return page

	def Repaginate():
		_apps.Sort()
		return Paginate(_numberOfItemsInFirstLine, _maxNumberOfLines)
